﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotvaBot.RequestChains;
using HttpRequester.RequestMakers.Base;
using HttpRequester;
using Helpers;
using mshtml;
using BotvaBot.RequestChainItems.Base;
using Common.Constants;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class BuyToolsChainItem : BotvaLogedInChainItemBase
    {
        protected override bool IsContextValidInternal()
        {
            return base.IsContextValidInternal();
        }

        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/mine.php";
            Method = HttpMethod.POST;
            AddGet("a", "shop");

            AddPost("type", "0");

            AddPost("items_buy[0]", "15");
            AddPost("items_buy[1]", "10");
            AddPost("items_buy[2]", "10");
            AddPost("items_buy[3]", "0");
            AddPost("items_buy[4]", "0");
            AddPost("items_buy[5]", "0");


            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            IHTMLFormElement form = null;
            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != "FORM")
                    continue;

                if (item.getAttribute("action", 0).ToString().ToUpper().Trim() != ""
                    || item.getAttribute("method", 0).ToString().ToUpper().Trim() != "POST")
                    continue;

                IHTMLElementCollection inputs = (item.all as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;
                foreach (IHTMLElement input in inputs)
                {
                    if (!(input is IHTMLInputElement))
                        continue;

                    IHTMLInputElement inp = input as IHTMLInputElement;
                    if (inp.type != null && inp.type.Trim().ToUpper() == "SUBMIT"
                        && inp.value != null && inp.value.Trim().ToUpper() == "КУПИТЬ")
                    {
                        form = item as IHTMLFormElement;
                        break;
                    }
                }

                if (form != null)
                    break;
            }

            IHTMLElementCollection formInputs = ((form as IHTMLElement).children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;
            foreach (IHTMLElement formItem in formInputs)
            {
                if (!(formItem is IHTMLInputElement))
                    continue;

                IHTMLInputElement formInput = formItem as IHTMLInputElement;
                if (formInput.type != null && formInput.type.Trim().ToUpper() == "HIDDEN"
                    && formInput.name != null && formInput.name.Trim().ToUpper() == "K"
                    && formInput.value != null)
                {
                    AddPost("k", formInput.value.Trim());
                }
            }

            AddPost("submit", "КУПИТЬ");
        }
    }
}
